﻿using Microsoft.AspNetCore.Mvc.Filters;
using System.Net.Sockets;
using System.Net;

namespace S_UserInfor.Read.Api
{
    public class MYIExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<MYIExceptionFilter> _logger;

        public MYIExceptionFilter(ILogger<MYIExceptionFilter> logger)
        {
            _logger = logger;
        }

        public void OnException(ExceptionContext context)
        {
            //获取本机ip
            var hostName = Dns.GetHostName();
            var hostEntry = Dns.GetHostEntry(hostName);
            var ipAdress = hostEntry.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
            _logger.LogError(ipAdress + context.Exception.StackTrace[..50] + "耿庆杰运行xxx任务发生了异常");
            context.ExceptionHandled = true;
        }
    }
}
